% Figure 2
% The consumer credit risk premium
% These panels plot the average consumer credit risk premium for the A,
% B, and C tranches, respectively. Averages are taken over all A, B, or C
% tranches in the sample for each month.


clear; clc;

% load data
opts = delimitedTextImportOptions("NumVariables", 8);
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
opts.VariableNames = ["Year", "Month", "SpreadPremium_A", "SpreadPremium_B", "SpreadPremium_C", "Crdspread_A", "Crdspread_B", "Crdspread_C"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
file = "IndexCardPremia.csv";
Premia= readtable(file, opts);
clear opts
Premia.Date = eomdate(Premia.Year,Premia.Month,'datetime');
Premia = table2timetable(Premia,'RowTimes','Date');
PlotData=Premia(:,["SpreadPremium_A","SpreadPremium_B","SpreadPremium_C"]);


fig1 = figure('NumberTitle', 'Off', 'Name', 'Figure 1');

figOrientation = 'portrait';
figWidth = 8.0; 
figHeight = 8.0; 

set(fig1,'Units','inch');
set(fig1,'Position',[0 0 figWidth figHeight]);
movegui(fig1,'center')


set(fig1,'PaperUnits', 'centimeters');
set(fig1,'PaperSize', [figWidth figHeight]);
set(fig1,'PaperPosition',[0 0 figWidth figHeight]);
set(fig1,'PaperOrientation',figOrientation);
set(fig1,'InvertHardcopy','on');


subplot_er(3,1,1);
ax = gca;
ax.PlotBoxAspectRatio = [1,0.35,0.35];
plot_1 = plot(datenum(PlotData.Date),PlotData.SpreadPremium_A);
set(plot_1, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
ylim([-100,1000])
minX = min(datenum(PlotData.Date));
maxX = max(datenum(PlotData.Date));
xlim([minX-0.025 maxX+0.025])
dateaxis
set(gca,'XTick',datenum([...
    'Jan-1-2002'; ...
    'Jan-1-2006'; ...
    'Jan-1-2010'; ...
    'Jan-1-2014'; ...
    'Jan-1-2018']));
set(gca,'XTickLabel',[...
    '2002';...
    '2006';...
    '2010';...
    '2014';...
    '2018'])
ylabel('Basis Points')
title('A Tranche','FontWeight','normal')
grid 'on';
box 'on';
set(gcf, 'Color', [1,1,1]);


subplot_er(3,1,2);
ax = gca;
ax.PlotBoxAspectRatio = [1,0.35,0.35];
plot_2 = plot(datenum(PlotData.Date),PlotData.SpreadPremium_B);
set(plot_2, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
minX = min(datenum(PlotData.Date));
maxX = max(datenum(PlotData.Date));
ylim([-200,2000])
xlim([minX-0.025 maxX+0.025])
dateaxis
set(gca,'XTick',datenum([...
    'Jan-1-2002'; ...
    'Jan-1-2006'; ...
    'Jan-1-2010'; ...
    'Jan-1-2014'; ...
    'Jan-1-2018']));
set(gca,'XTickLabel',[...
    '2002';...
    '2006';...
    '2010';...
    '2014';...
    '2018'])
ylabel('Basis Points')
title('B Tranche','FontWeight','normal')
grid 'on';
box 'on';
set(gcf, 'Color', [1,1,1]);



subplot_er(3,1,3);
ax = gca;
ax.PlotBoxAspectRatio = [1,0.35,0.35];
plot_3 = plot(datenum(PlotData.Date),PlotData.SpreadPremium_C);
set(plot_3, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
minX = min(datenum(PlotData.Date));
maxX = max(datenum(PlotData.Date));
xlim([minX-0.025 maxX+0.025])
ylim([-300,3000])
dateaxis
set(gca,'XTick',datenum([...
    'Jan-1-2002'; ...
    'Jan-1-2006'; ...
    'Jan-1-2010'; ...
    'Jan-1-2014'; ...
    'Jan-1-2018']));
set(gca,'XTickLabel',[...
    '2002';...
    '2006';...
    '2010';...
    '2014';...
    '2018'])
ylabel('Basis Points')
title('C Tranche','FontWeight','normal')
grid 'on';
box 'on';
set(gcf, 'Color', [1,1,1]);


